/**    
 * 文件名：RechargePayRequestHandler.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年11月28日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.models.payment.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.LandeaConstants.AccountStatus;
import com.logic.landseaserver.common.enums.ContractStatusCodeEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.PaymentResultCode;
import com.logic.landseaserver.domain.Account;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.domain.Contract;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.BillTypeCode;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;
import com.logic.landseaserver.models.payment.service.AbstractPaymentRequestHandler;
import com.logic.landseaserver.persistence.read.AccountReadMapper;
import com.logic.landseaserver.persistence.read.ContractReadMapper;
import com.logic.landseaserver.persistence.write.BillPaymentWriteMapper;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：RechargePayRequestHandler</br>
 * 类描述：账户充值支付</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年11月28日 上午11:46:13</br>
 * 
 * @version 1.0
 *
 */
@Service(BillTypeCode.RECHARGE + PaymentConstant.PAYMENT_REQUEST_SUFFIX)
public class RechargePayRequestHandler extends AbstractPaymentRequestHandler
{
    @Autowired
    private BillPaymentWriteMapper billPayWriteMapper;
    
    @Autowired
    private AccountReadMapper accountReadMapper;
    
    @Autowired
    private ContractReadMapper contractReadMapper;
    
    /**
     * [描述]：支付之前check金额是否正确等安全性校验</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void customCheckPayParam(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        // 检查输入金额是否合法
        Double payMoney = payment.getMoney();
        
        String logInfo = getLog(payment, helperObject.getPaymentId());
        if (payMoney <= 0.00d)
        {
            // 支付金额小于等于0，非法
            LOGGER.error(logInfo + " Pay money is invalied,payMoney=" + payMoney);
            throw LandseaException.createException(PaymentResultCode.E00140006);
        }
        
        // 检查账户是否存在
        Account account = accountReadMapper.selectByAccountBalanceId(payment.getBillId());
        if (null == account || account.getDeleted())
        {
            throw LandseaException.createException(PaymentResultCode.E00140010);
        }
        
        // 如果账户不正常，则不能充值
        if (!AccountStatus.NORMAL.equals(account.getStatus()))
        {
            LOGGER.error(logInfo + "租客账户冻结，不能充值.status=" + account.getStatus());
            throw LandseaException.createException(PaymentResultCode.E00150034);
        }
        
        // 检查账户下是否有有效的合同
        // 查询合同ID
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", account.getUserId());
        paramMap.put("roomId", account.getRoomId());
        List<Contract> contractList = contractReadMapper.queryContractWithUserRoom(paramMap);
        
        if (CollectionUtils.isEmpty(contractList))
        {
            LOGGER.error(logInfo + "账户下不存在有效合同,不能充值.accountId=" + account.getId());
            throw LandseaException.createException(PaymentResultCode.E00150033);
        }
        
        boolean isHaveSignContract = false;
        for (Contract contract : contractList)
        {
            if (ContractStatusCodeEnum.SIGN.getCode().equals(contract.getStatus()))
            {
                isHaveSignContract = true;
            }
        }
        if (!isHaveSignContract)
        {
            LOGGER.error(logInfo + "账户下不存在有效合同,不能充值.accountId=" + account.getId());
            throw LandseaException.createException(PaymentResultCode.E00150033);
        }
        
        helperObject.setProjectId(account.getProjectId());
    }
    
    /**
     * [描述]：业务逻辑检查</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void checkPayBillAndMoney(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void doLocalBusiness(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        billPayWriteMapper.insertSelective(billPayment);
        
        // 将paymentId重新赋值
        helperObject.setPaymentId(billPayment.getId());
        resultMap.put("paymentId", billPayment.getId());
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @return
     *
     */
    @Override
    public Integer getPaymentId(BillPayment billPayment, PaymentHelperBean helperObject)
    {
        return helperObject.getPaymentId();
    }
    
    /**
     * [描述]：</br>
     *
     * @return
     *
     */
    @Override
    public String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }
}
